class Solution {
public:
    int totalFruit(vector<int>& fruits) 
    {
        int hash[100001]={0};
        int left=0,right=0,ret=0,kind=0; 
        for (;right<fruits.size();right++)
        {
            hash[fruits[right]]++;
            if (hash[fruits[right]]==1)
                kind++;
            while (kind==3)
            {
                hash[fruits[left++]]--;
                if (hash[fruits[left-1]]==0)
                    kind--;
            }
            ret=max(ret,right-left+1);
        }
        return ret;
    }
};
